//
//  GPAlbumPickerViewController.m
//  GP
//
//  Created by Sally on 16/8/4.
//  Copyright © 2016年 Sally. All rights reserved.
//

#import "GPAlbumPickerViewController.h"
#import "LYPhotoTools.h"
#import <MobileCoreServices/UTCoreTypes.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "GPPhotoViewController.h"
@interface GPAlbumPickerViewController()
@property (nonatomic, strong) ALAssetsLibrary *library;

@end
@implementation GPAlbumPickerViewController{
    NSArray *albumPictureSet;
    LYPhotoTools *photoTool;
}

- (void)viewDidLoad{
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.navigationItem setTitle:@"加载中..."];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelImagePicker)];
    [self.navigationItem setRightBarButtonItem:cancelButton];
    [self getAlbumPitures];
   
}

- (void)getAlbumPitures{
     photoTool = [[LYPhotoTools alloc] init];
    __weak typeof(self) weakSelf = self;
    [photoTool getAlbumPicture:^(NSArray *array) {
        albumPictureSet = array;
        [weakSelf reloadTableView];
    }];
}

- (void)reloadTableView
{
    [self.tableView reloadData];
    [self.navigationItem setTitle:@"照片"];
}


- (ALAssetsFilter *)assetFilter
{
    return [ALAssetsFilter allAssets];
    
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [albumPictureSet count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // Get count
    ALAssetsGroup *g = [albumPictureSet objectAtIndex:indexPath.row];
    [g setAssetsFilter:[self assetFilter]];
    NSInteger gCount = [g numberOfAssets];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%ld)",[g valueForProperty:ALAssetsGroupPropertyName], (long)gCount];
    UIImage* image = [UIImage imageWithCGImage:[g posterImage]];
    image = [self resize:image to:CGSizeMake(78, 78)];
    [cell.imageView setImage:image];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 95;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    GPPhotoViewController *picker = [[GPPhotoViewController alloc] initWithNibName:@"GPPhotoViewController" bundle:nil];
    picker.assetGroup = [albumPictureSet objectAtIndex:indexPath.row];
    [picker.assetGroup setAssetsFilter:[self assetFilter]];
    
    ALAssetsGroup *g = (ALAssetsGroup*)[albumPictureSet objectAtIndex:indexPath.row];
    [g setAssetsFilter:[self assetFilter]];
    NSInteger gCount = [g numberOfAssets];
    picker.title = [g valueForProperty:ALAssetsGroupPropertyName];
    picker.allOfImage = (int)gCount;
    [self.navigationController pushViewController:picker animated:YES];
}


- (UIImage *)resize:(UIImage *)image to:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (void)cancelImagePicker{
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
